Grokking JavaScript Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who should take this course?
Course Overview
Introduction to JavaScript
JavaScript Basics
JavaScript - Hello World
JavaScript - Comments
Quiz
JavaScript Variables and Operators
JavaScript - Variables
JavaScript - Operators
Quiz
JavaScript Conditionals
Introduction to Conditional Statements in JavaScript
JavaScript - If...Else
JavaScript - Switch Case
Quiz
JavaScript Objects
Introduction to Objects in JavaScript
JavaScript - Number
JavaScript - Boolean
JavaScript - Math
JavaScript - Date
Quiz
JavaScript - Functions
Introductions to Function in JavaScript
JavaScript - Variable Scope
JavaScript - Default Parameters
JavaScript - Arrow Functions
Quiz
JavaScript - String and Arrays
Introduction to Strings in JavaScript
JavaScript - String Methods
Introduction to Array in JavaScript
JavaScript - Array Methods
Quiz
Introduction to loops in JavaScript
JavaScript - for loop
JavaScript - while loop
JavaScript - do-while loop
JavaScript - for-in loop
JavaScript - for-of loop
JavaScript - Loop Control
Quiz
JavaScript - Maps
JavaScript - Sets
Quiz
Where to Go from Here
JavaScript - Array Methods

JavaScript arrays are flexible and powerful, capable of storing multiple values in an ordered list. They come with a variety of built-in properties and methods that allow developers to manipulate these lists in nearly any conceivable way—adding, removing, sorting, searching, slicing, and more. Understanding these methods and properties is essential for efficient data handling and manipulation in JavaScript.

Array Properties

JavaScript arrays offer several useful properties:

Sr.NoPropertyDescription
1lengthReturns the number of elements in the array.

Array Methods

Here's an overview of some commonly used array methods:

Sr.NoMethod NameDescription
1at()Retrieves an element at a given index, allowing for positive and negative integers.
2concat()Combines the array with other array(s) or value(s) and returns a unified array.
3copyWithin()Copies array elements within the array to and from specified positions.
4entries()Provides an iterator that contains key/value pairs for each index in the array.
5every()Checks if all elements in the array pass a test defined in a provided function.
6fill()Fills all the elements of an array from a start index to an end index with a static value.
7filter()Creates a new array with elements that pass the test provided by a function.
8find()Finds the first element in the array that satisfies the provided testing function.
9findIndex()Finds the index of the first element in the array that satisfies the provided testing function.
10findLast()Finds the last element in the array that satisfies the provided testing function.
11findLastIndex()Finds the index of the last element in the array that satisfies the provided testing function.
12flat()Flattens the array to the specified depth.
13flatMap()First flattens the array a single level deep, then maps each element using a mapping function.
14forEach()Executes a provided function once for each array element.
15includes()Determines whether an array includes a certain value, returning true or false.
16indexOf()Finds the first index of a given element in the array, or -1 if it is not present.
17join()Joins all elements of an array into a string and returns this string.
18keys()Returns a new array iterator that contains the keys for each index in the array.
19lastIndexOf()Returns the last index at which a given element can be found in the array, or -1 if it is not present.
20map()Creates a new array populated with the results of calling a provided function on every element in the array.
21pop()Removes the last element from an array and returns that element.
22push()Adds one or more elements to the end of an array and returns the new length of the array.
23reduce()Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
24reduceRight()Applies a function against an accumulator and each element in the array (from right to left) to reduce it to a single value.
25reverse()Reverses the order of the elements in an array in place.
26shift()Removes the first element from an array and returns that removed element.
27slice()Returns a shallow copy of a portion of an array into a new array object.
28some()Tests whether at least one element in the array passes the test implemented by the provided function.
29toSource()Generates a string representing the source code of the array.
30sort()Sorts the elements of an array in place and returns the array.
31splice()Changes the contents of an array by removing or replacing existing elements and/or adding new elements.
32toLocaleString()Converts the elements of the array to strings and concatenates them, separated by a locale-specific String.
33toReversed()Creates a new array with the elements of the calling array reversed.
34toSpliced()Creates a new array by removing, replacing, or adding elements at a specific index.
35toString()Converts an array and its elements to a string.
36unshift()Adds one or more elements to the beginning of an array and returns the new length of the array.
37values()Returns a new array iterator object that contains the values for each index in the array.
38with()Returns a new array with an element at a given index replaced with a new value.

Examples

Example 1: Using the length Property

In this example, we'll determine the number of elements in an array.

Javascript

. . . .
  • let fruits = ["Apple", "Banana", "Cherry"]; creates an array named fruits with three elements.
  • console.log(fruits.length); logs the number of elements in the fruits array to the console. The length property returns 3, indicating there are three elements in the array.

Example 2: Adding Elements with push() Method

In this example, we'll add a new element to the end of an array.

Javascript

. . . .
  • fruits.push("Durian"); adds "Durian" to the end of the fruits array.
  • The push() method increases the array's length by one.
  • Logging the fruits array now shows four elements, including the newly added "Durian".

Example 3: Removing Elements with pop() Method

In this example, we'll remove the last element from an array.

Javascript

. . . .
  • let removedFruit = fruits.pop(); removes the last element from the fruits array and stores it in removedFruit.
  • The pop() method decreases the array's length by one and returns the removed element.
  • Logging removedFruit shows "Cherry", the element that was removed.
  • The fruits array now only contains two elements, as "Cherry" has been removed.

These examples illustrate how to use the length property to determine the size of an array, and the push() and pop() methods to add and remove elements, respectively. Understanding and effectively utilizing these array properties and methods is crucial for manipulating lists of data in JavaScript.

Introduction to Array in JavaScript
Quiz
Mark as Completed

On this page

Array Properties

Array Methods

Examples

Example 1: Using the length Property

Example 2: Adding Elements with push() Method

Example 3: Removing Elements with pop() Method